package yns.springboot.common.sample.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yns.springboot.common.sample.base.service.impl.BaseServiceImpl;
import yns.springboot.common.sample.dao.UserDao;
import yns.springboot.common.sample.dto.RoleDto;
import yns.springboot.common.sample.dto.UserDto;
import yns.springboot.common.sample.entity.RoleEntity;
import yns.springboot.common.sample.entity.UserEntity;
import yns.springboot.common.sample.page.PaginationInfo;
import yns.springboot.common.sample.service.UserService;

@Service
public class UserServiceImpl extends BaseServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	@Override
	public List<UserDto> getUser() {
		List<UserEntity> listUserEntity = userDao.getUser();
		List<UserDto> listUserDto = new ArrayList<UserDto>();

		listEntityToDto(listUserEntity, listUserDto);
		
		return listUserDto;
	}

	@Override
	public int addUser(UserDto userDto) {
		UserEntity userEntity = new UserEntity();
		transformDtoToEntity.parseObject(userDto, userEntity);
		
		return userDao.addUser(userEntity);

	}

	@Override
	public UserDto getUserByName(String userName) {
		UserEntity userEntity = userDao.getUserByName(userName);
		UserDto userDto = new UserDto();
		transformEntityToDto.parseObject(userEntity, userDto);
		
		return userDto;
	}

	@Override
	public int deleteUser(int id) {
		return userDao.deleteUser(id);

	}

	@Override
	public int updateUser(UserDto userDto) {
		UserEntity userEntity = new UserEntity();
		transformDtoToEntity.parseObject(userDto, userEntity);
		
		return userDao.updateUser(userEntity);
	}

	@Override
	public int getTotalCount() {
		return userDao.getTotalCount();
	}

	@Override
	public List<UserDto> getPage(int currentPage, int pageSize) {
		
		PaginationInfo<UserEntity> paginationInfo = new PaginationInfo<UserEntity>();
		paginationInfo.setCurrentPage(currentPage);
		paginationInfo.setPageSize(pageSize);

		PaginationInfo<UserEntity> pagingResults = pagination.paging(userDao, paginationInfo);
		List<UserEntity> listRoleEntity = pagingResults.getDataList();
		
		List<UserDto> listRoleDto = new ArrayList<UserDto>();
		
		listEntityToDto(listRoleEntity, listRoleDto);
		
		
		return listRoleDto;
	}

	@Override
	public void listEntityToDto(List<UserEntity> listEntity, List<UserDto> listDto) {
		UserDto dto;
		for (UserEntity entity : listEntity) {
			dto = new UserDto();
			transformEntityToDto.parseObject(entity, dto);
			listDto.add(dto);
		}

	}

	@Override
	public void listDtoToEntity(List<UserDto> listDto, List<UserEntity> listEntity) {
		UserEntity entity;
		for (UserDto dto : listDto) {
			entity = new UserEntity();
			transformDtoToEntity.parseObject(dto, entity);
			listEntity.add(entity);
		}

	}

	@Override
	public List<UserDto> search(int currentPage, int pageSize, UserDto dto) {
		// TODO Auto-generated method stub
		return null;
	}

}
